package com.jdls.web;

import com.alibaba.fastjson.JSON;
import com.jdls.pojo.Province;
import com.jdls.service.ProvinceService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 嘉德罗斯
 * @date 2021/9/6
 */
@WebServlet("/ProvinceServlet")
public class ProvinceServlet extends HttpServlet {
    private ProvinceService provinceService = new ProvinceService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8"); // 处理请求参数的乱码
        response.setContentType("text/json;charset=utf-8"); // 处理响应数据的乱码
        List<Province> provinces = provinceService.selectAll();
        String s = JSON.toJSONString(provinces);
        response.getWriter().write(s);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
